package com.snail.poker.web.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 基本的Action
 * 
 * @author Snail
 * 
 */
public abstract class BaseAction extends ActionSupport implements SessionAware,
		ServletResponseAware, ServletRequestAware {

	private static final long serialVersionUID = 1L;
	HttpServletRequest request;
	HttpServletResponse response;
	Map<String, Object> session;
	/**
	 * 验证标识
	 */
	protected boolean success;
	/**
	 * 返回客户端错误信息
	 */
	protected String message;

	public BaseAction() {
		super();
		this.success = true;
		this.message = "";
	}

	public boolean getSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * Request
	 */
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	/**
	 * response
	 */
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}

	/**
	 * session
	 */
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	/**
	 * 查询数据详情
	 * 
	 * @return
	 */
	public String detail() {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	/**
	 * 表单提交操作（数据新增 或 数据修改）
	 * 
	 * @return
	 */
	public String input() {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	/**
	 * 数据查询操作
	 * 
	 * @return
	 */
	public String query() {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	/**
	 * 数据删除操作
	 * 
	 * @return
	 */
	public String remove() {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	/**
	 * 加入返回错误标识的消息内容
	 * 
	 * @param message
	 *            消息内容
	 */
	protected void putMessage(String message) {
		this.success = false;
		this.message = message;
	}
	
	/**
	 * 获取请求客户端的IP地址
	 * 
	 * @return
	 */
	protected String getIpAddress() {
		String ip = this.request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = this.request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = this.request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = this.request.getRemoteAddr();
		}
		return ip;
	}
}